var e = require;
var t = module;
var o = exports;
var n,
    i = o,
    a =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    r = function (e, t, o, n) {
        var i,
            a = arguments.length,
            r = a < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, o)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(e, t, o, n);
        else
            for (var s = e.length - 100; s >= 0; s--)
                (i = e[s]) && (r = (a < 3 ? i(r) : a > 3 ? i(t, o, r) : i(t, o)) || r);
        return a > 3 && r && Object.defineProperty(t, o, r), r;
    };
Object.defineProperty(i, "__esModule", {value: !0});
var s = e("AssetsMap"),
    c = e("BundleNames"),
    l = e("GlobalParam"),
    d = e("Util"),
    u = e("ConfigGameData"),
    p = e("GameData"),
    h = e("GameOverData"),
    f = e("nodePoolManager"),
    _ = e("SkinLevelData"),
    y = e("TaskData"),
    m = e("GameModuleView"),
    g = e("snakeControl"),
    v = cc._decorator,
    w = v.ccclass,
    b =
        (v.property,
        (function (e) {
            function t() {
                var t = (null !== e && e.apply(this, arguments)) || this;
                return (t.snakeList = []), (t.playerControl = null), (t.isStartTesting = !1), (t.isGameVoer = !1), t;
            }
            return (
                a(t, e),
                (t.prototype.start = function () {
                    var e = this,
                        t = _.SkinLevelData.instance.skinList[p.default.instance.UserSkin];
                    (p.default.instance.initlength = t.initlength[t.level]),
                        (p.default.instance.eatSize = t.eatSize[t.level]),
                        t.profit.length > t.level && (p.default.instance.profit = t.profit[t.level]),
                        this.scheduleOnce(function () {
                            var t = p.default.instance.initlength + 1;
                            f.default.instance.gameModelInstance.param.score &&
                                (t = f.default.instance.gameModelInstance.param.score),
                                e.createSnake(
                                    p.default.instance.UserName,
                                    p.SnakeType.Player,
                                    p.default.instance.UserSkin,
                                    cc.v3(0, 0, 0),
                                    0,
                                    t
                                );
                        }, 0),
                        this.startCreateSnake(),
                        this.scheduleOnce(function () {
                            e.isStartTesting = !0;
                        }, 3);
                }),
                (t.prototype.startCreateSnake = function () {
                    var e = this;
                    this.scheduleOnce(function () {
                        e.timingAddSnake(1);
                    }, 0.1);
                }),
                (t.prototype.newSnake = function (e, t) {
                    var o = this;
                    void 0 === t && (t = 5),
                        this.scheduleOnce(function () {
                            if (p.default.instance.gameState == p.GameState.play) {
                                var t = o.snakeList[e];
                                (t.head_pos = u.default.GetAIPos(e)),
                                    (t.score =
                                        e <= 1
                                            ? d.default.randomFrom(1e5, 13e4)
                                            : e < 3
                                            ? d.default.randomFrom(3e4, 5e4)
                                            : e < 5
                                            ? d.default.randomFrom(8e3, 2e4)
                                            : d.default.randomFrom(50, 1e3)),
                                    p.default.instance.GameModel == p.GameModel.TimeLimit && (t.score = 100),
                                    t.init();
                            }
                        }, t);
                }),
                (t.prototype.timingAddSnake = function (e) {
                    var t = this;
                    if (e != l.default.GameConfigData.SnakeMapNumber) {
                        var o = u.default.GetAIPos(e),
                            n = u.default.instance.GetAIName(e),
                            i = 100;
                        i =
                            e <= 1
                                ? d.default.randomFrom(12e4, 14e4)
                                : e < 3
                                ? d.default.randomFrom(4e4, 6e4)
                                : e < 5
                                ? d.default.randomFrom(15e3, 3e4)
                                : d.default.randomFrom(50, 1e3);
                        var a = d.default.randomFrom(0, _.SkinLevelData.instance.skinList.length - 1);
                        if (
                            (a >= _.SkinLevelData.instance.skinList.length &&
                                (a -= _.SkinLevelData.instance.skinList.length),
                            f.default.instance.gameModelInstance instanceof m.default &&
                                m.default.Insance.param &&
                                m.default.Insance.param.aiData)
                        ) {
                            var r = m.default.Insance.param.aiData[e - 1];
                            r && r.name && r.score && ((n = r.name), (i = r.score));
                        }
                        p.default.instance.GameModel == p.GameModel.TimeLimit && (i = 100),
                            this.createSnake(n, p.SnakeType.AI, a, o, e, i),
                            this.scheduleOnce(function () {
                                t.timingAddSnake(e + 1);
                            }, 0.1);
                    }
                }),
                (t.prototype.createSnake = function (e, t, o, n, i, a) {
                    void 0 === t && (t = p.SnakeType.None),
                        void 0 === o && (o = 0),
                        void 0 === n && (n = cc.v3(0, 0, 0)),
                        void 0 === i && (i = 0),
                        void 0 === a && (a = 100);
                    var r = game.UIManager.instance.createNodeByPreloading(
                        c.default.Game,
                        s.default.GameBundle.prefabs.assetsList.snakePrefab
                    );
                    r.parent = this.node;
                    var l = r.getComponent(g.default);
                    l || (l = r.addComponent(g.default)),
                        (l.snakeName = e),
                        (l.skin_index = o),
                        (l.snakeType = t),
                        (l.score = a),
                        (l.head_pos = n),
                        (l.snake_index = i),
                        l.init(),
                        this.snakeList.push(l);
                }),
                (t.prototype.bomb_burst = function (e, t) {
                    for (var o, n = 0, i = this.snakeList.length; n < i; n++)
                        if (this.snakeList[n].snakeName == t) {
                            o = this.snakeList[n];
                            break;
                        }
                    for (n = 0, i = this.snakeList.length; n < i; n++)
                        if (
                            this.snakeList[n].snakeName != t &&
                            !this.snakeList[n]._isShield &&
                            !this.snakeList[n].isIgnore &&
                            !this.snakeList[n].isDie &&
                            this.snakeList[n].node.active
                        ) {
                            var a = this.snakeList[n].snakeSkinControl.snake_head
                                .getChildByName("image")
                                .getChildByName("target_eat")
                                .convertToWorldSpaceAR(cc.Vec2.ZERO);
                            this.node.parent.convertToNodeSpaceAR(a).sub(cc.v2(e.x, e.y)).mag() < 300 &&
                                ((this.snakeList[n].life -= l.default.GameConfigData.reduceStars),
                                this.snakeList[n].eatPorp_Stars(!1),
                                this.snakeList[n].life <= 0 &&
                                    (f.default.instance.gameModelInstance._view_killList.refreshSnakeKillData(
                                        this.snakeList[n],
                                        o
                                    ),
                                    this.snakeList[n].onSnakeDie()));
                        }
                }),
                (t.prototype.updateF = function () {
                    if (p.default.instance.gameState == p.GameState.play) {
                        for (var e = m.default.Insance.snakeGridMap, t = 0, o = this.snakeList.length; t < o; t++)
                            if (
                                !this.snakeList[t].isDie &&
                                (this.snakeList[t].head_pos.sub(cc.v3(0, 0, 0)).mag() >
                                    p.default.instance.MapSize / 2 - 20 && this.snakeList[t].onSnakeDie(),
                                !this.snakeList[t]._isShield && !this.snakeList[t].isIgnore)
                            ) {
                                var n = e.getBlockInfosBySize(this.snakeList[t].head_pos, 2),
                                    i = this.checkCollision(this.snakeList[t], n);
                                i &&
                                    (y.default.instance.collectSnakeDieData(this.snakeList[t].snakeName, i.snakeName),
                                    m.default.Insance._view_killList.refreshSnakeKillData(this.snakeList[t], i),
                                    h.default.instance.set_killData(this.snakeList[t], i),
                                    this.snakeList[t].onSnakeDie());
                            }
                        if (p.default.instance.GameModel == p.GameModel.eatChicken) {
                            t = 0;
                            for (var a = this.snakeList.length; t < a; t++)
                                if (
                                    !this.snakeList[t]._isShield &&
                                    !this.snakeList[t].isIgnore &&
                                    !this.snakeList[t].isDie &&
                                    this.snakeList[t].node.active
                                ) {
                                    var r = this.snakeList[t].head_pos,
                                        c = m.default.Insance._poisonMap.mask.position;
                                    r.sub(c).mag() >= m.default.Insance._poisonMap.mapSize / 2 &&
                                        ((this.snakeList[t].life -= d.default.randomFrom(1, 5)),
                                        this.snakeList[t].life <= 0 && this.snakeList[t].onSnakeDie());
                                }
                            if (this.isGameVoer) return;
                            if (!this.isStartTesting) return;
                            var l = 0,
                                u = !1,
                                f = "";
                            for (t = 0, a = this.snakeList.length; t < a; t++)
                                !this.snakeList[t].isDie && this.snakeList[t].node.active
                                    ? (l++, (f = this.snakeList[t].snakeName))
                                    : this.snakeList[t].snakeName == p.default.instance.UserName && (u = !0);
                            1 != l ||
                                f != p.default.instance.UserName ||
                                u ||
                                ((this.isGameVoer = !0),
                                game.UIManager.instance.showToast("吃鸡成功"),
                                setTimeout(function () {
                                    ad.curr_platform == window.ad.Platform.OPPO && ad.adInterface.isNativePatchReady()
                                        ? game.PopupManager.instance.showPopView(
                                              s.default.PopupMap.SettlementView2,
                                              null,
                                              !1,
                                              !1
                                          )
                                        : game.PopupManager.instance.showPopView(
                                              s.default.PopupMap.SettlementView,
                                              null,
                                              !1,
                                              !1
                                          );
                                }, 1e3)),
                                u &&
                                    ((this.isGameVoer = !0),
                                    game.UIManager.instance.showToast("吃鸡失败"),
                                    setTimeout(function () {
                                        ad.curr_platform == window.ad.Platform.OPPO &&
                                        ad.adInterface.isNativePatchReady()
                                            ? game.PopupManager.instance.showPopView(
                                                  s.default.PopupMap.SettlementView2,
                                                  null,
                                                  !1,
                                                  !1
                                              )
                                            : game.PopupManager.instance.showPopView(
                                                  s.default.PopupMap.SettlementView,
                                                  null,
                                                  !1,
                                                  !1
                                              );
                                    }, 1e3));
                        }
                    }
                }),
                (t.prototype.checkCollision = function (e, t) {
                    if (t.length <= 0) return null;
                    for (var o = 0; o < t.length; o++) {
                        var n = t[o].getFirstBlockItem(e.snakeName);
                        if (
                            n &&
                            !n.snakeControl.isDie &&
                            n.snakeControl &&
                            n.snakeControl.head_pos &&
                            !n.snakeControl._isShield &&
                            !n.snakeControl.isIgnore
                        ) {
                            var i = m.default.Insance.snakeGridMap.getCenterPosByIndex(t[o].index);
                            if (e.head_pos.sub(i).mag() < e.snake_size + n.snakeControl.snake_size)
                                return n.snakeControl;
                        }
                    }
                    return null;
                }),
                r([w], t)
            );
        })(cc.Component));
i.default = b;
